import java.util.Arrays;

/**
 * 611. 有效三角形的个数
 * https://leetcode.cn/problems/valid-triangle-number/description/
 */
class Solution {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length, ret = 0;
        for(int i = n - 1; i >= 2; i--) {
            int left = 0, right = i - 1;
            while(left < right) {
                if(nums[left] + nums[right] > nums[i]) {
                    ret += right - left;
                    right--;
                }else {
                    left++;
                }
            }
        }
        return ret;
    }
}